import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.*;
import javafx.stage.Stage;

import java.util.Random;

public class homework_11 extends Application {
    private static final double WIDTH = 800;
    private static final double HEIGHT = 600;
    private final Random random = new Random();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Canvas canvas = new Canvas(WIDTH, HEIGHT);
        GraphicsContext gc = canvas.getGraphicsContext2D();

        // 绘制渐变背景
        drawGradientBackground(gc);

        // 自动计算坐标系范围
        double mathXMin = -200;
        double mathXMax = 200;
        double mathYMin = -200;
        double mathYMax = 200;

        // 坐标转换参数
        double scaleX = WIDTH / (mathXMax - mathXMin);
        double scaleY = HEIGHT / (mathYMax - mathYMin);

        // 绘制3朵云
        drawCloud(gc, 100, 100, 1.0);
        drawCloud(gc, 500, 150, 0.8);
        drawCloud(gc, 300, 80, 1.2);

        // 绘制12瓣彩色玫瑰线
        drawMultiColoredRose(gc, mathXMin, mathXMax, scaleX, scaleY, mathYMin);

        primaryStage.setScene(new Scene(new StackPane(canvas), WIDTH, HEIGHT));
        primaryStage.setTitle("彩色玫瑰与云朵");
        primaryStage.show();
    }

    // 绘制渐变背景
    private void drawGradientBackground(GraphicsContext gc) {
        Stop[] stops = new Stop[]{
                new Stop(0, Color.LIGHTSKYBLUE),
                new Stop(0.5, Color.LIGHTBLUE),
                new Stop(1, Color.DEEPSKYBLUE)
        };
        LinearGradient gradient = new LinearGradient(0, 0, 0, 1, true, CycleMethod.NO_CYCLE, stops);
        gc.setFill(gradient);
        gc.fillRect(0, 0, WIDTH, HEIGHT);
    }

    // 绘制云朵
    private void drawCloud(GraphicsContext gc, double centerX, double centerY, double scale) {
        gc.setFill(Color.WHITE);
        gc.beginPath();

        // 云朵主体由多个椭圆组成
        double baseRadius = 30 * scale;

        // 主椭圆
        gc.moveTo(centerX, centerY);
        gc.arc(centerX, centerY, baseRadius, baseRadius * 0.6, 0, 360);

        // 周围的小椭圆
        gc.arc(centerX - baseRadius * 0.7, centerY - baseRadius * 0.3,
                baseRadius * 0.7, baseRadius * 0.5, 0, 360);
        gc.arc(centerX + baseRadius * 0.7, centerY - baseRadius * 0.2,
                baseRadius * 0.6, baseRadius * 0.5, 0, 360);
        gc.arc(centerX - baseRadius * 0.3, centerY + baseRadius * 0.2,
                baseRadius * 0.5, baseRadius * 0.4, 0, 360);
        gc.arc(centerX + baseRadius * 0.4, centerY + baseRadius * 0.3,
                baseRadius * 0.6, baseRadius * 0.5, 0, 360);

        gc.closePath();
        gc.fill();

        // 添加云朵阴影效果
        gc.setFill(Color.rgb(255, 255, 255, 0.7));
        gc.fillOval(centerX - baseRadius * 0.5, centerY + baseRadius * 0.2,
                baseRadius * 1.2, baseRadius * 0.4);
    }

    // 绘制多色玫瑰线
    private void drawMultiColoredRose(GraphicsContext gc, double mathXMin, double mathXMax,
                                      double scaleX, double scaleY, double mathYMin) {
        final int PETALS = 12;
        final int SEGMENTS_PER_PETAL = 30;
        final double a = 150;
        final double k = PETALS / 2.0;

        Color[] colors = {
                Color.rgb(255, 50, 50, 0.8), Color.rgb(255, 150, 50, 0.8),
                Color.rgb(255, 255, 50, 0.8), Color.rgb(50, 255, 50, 0.8),
                Color.rgb(50, 150, 255, 0.8), Color.rgb(100, 50, 255, 0.8),
                Color.rgb(255, 50, 150, 0.8), Color.rgb(255, 100, 180, 0.8),
                Color.rgb(50, 255, 200, 0.8), Color.rgb(200, 50, 255, 0.8),
                Color.rgb(255, 200, 50, 0.8), Color.rgb(150, 255, 50, 0.8)
        };

        for (int petal = 0; petal < PETALS; petal++) {
            gc.setFill(colors[petal % colors.length]);
            gc.beginPath();

            double startAngle = 2 * Math.PI * petal / PETALS;
            double endAngle = 2 * Math.PI * (petal + 1) / PETALS;

            for (int i = 0; i <= SEGMENTS_PER_PETAL; i++) {
                double theta = startAngle + (endAngle - startAngle) * i / SEGMENTS_PER_PETAL;
                double r = a * Math.cos(k * theta);

                double x = r * Math.cos(theta);
                double y = r * Math.sin(theta);

                double screenX = (x - mathXMin) * scaleX;
                double screenY = HEIGHT - ((y - mathYMin) * scaleY);

                if (i == 0) {
                    gc.moveTo((0 - mathXMin) * scaleX, HEIGHT - ((0 - mathYMin) * scaleY));
                    gc.lineTo(screenX, screenY);
                } else {
                    gc.lineTo(screenX, screenY);
                }
            }

            gc.lineTo((0 - mathXMin) * scaleX, HEIGHT - ((0 - mathYMin) * scaleY));
            gc.closePath();
            gc.fill();

            gc.setStroke(Color.rgb(0, 0, 0, 0.3));
            gc.setLineWidth(1);
            gc.stroke();
        }
    }
}